package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;
import com.xie.pojo.Potion;
import com.xie.service.PotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "药品管理")
@RestController
@RequestMapping("/Potions")
public class PotionController {
    @Autowired
    private PotionService potionService;

    /**
     * 分页查询
     */
    @GetMapping("/{pageIndex}/{pageSize}")
    @ApiOperation("分页查询药品信息")
    public Result getPage(@PathVariable Integer pageIndex,
                          @PathVariable  Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage<Potion> page1 = potionService.page(page);
        if (page1 != null) return Result.ok(page1);
        return Result.fail();
    }

    /**
     * 模糊查询
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询药品信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Potion> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Potion::getPotionNo,params)
                .or().like(Potion::getPotionId,params)
                .or().like(Potion::getPotionName,params)
                .or().like(Potion::getPotionPrice,params)
                .or().like(Potion::getPotionQuantity,params)
                .or().like(Potion::getPotionRen,params)
        ;
        List<Potion> list = potionService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加药品
     * @param potion
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("添加药品信息")
    public Result addPersonnel(@RequestBody Potion potion) {
        boolean save = potionService.save(potion);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除药品
     * @param id
     * @return
     */
    @PostMapping("/removeById/{id}")
    @ApiOperation("根据id删除药品信息")
    public Result deletePotion(@PathVariable Long id) {
        boolean save = potionService.removeById(id);
        if (save) return Result.ok();
        return Result.fail();
    }


    /**
     * 修改药品
     * @param potion
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改药品信息")
    public Result updatePotion(@RequestBody Potion potion) {
        boolean update = potionService.updateById(potion);
        if (update) return Result.ok();
        return Result.fail();
    }


    /**
     * 根据id查询
     * @param potionId
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询药品信息")
    public Result findPotionById(@PathVariable Long id) {
        Potion byId = potionService.getById(id);
        if (byId != null) {
            return Result.ok(byId);
        }
        return Result.fail();
    }



    /**
     * 查询全部
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有药品信息")
    public Result exportPersonnel(){
        List<Potion> list = potionService.list();
        if (list!= null) {
            return Result.ok(list);
        }
        return Result.fail();
    }
}
